import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-08-15
 * Time:21:06
 */
public class JDBCUpdate {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java105?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("wgz20030828");

        //连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);

        //输入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要修改的学生的学号:");
        int id = scanner.nextInt();
        System.out.println("请输入修改后的学生的姓名:");
        String name = scanner.next();


        // ? 作为占位符. 占个位置, 后面会替换成其他的值.
        String sql ="update student set name = ? where id = ?";
        //预处理
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //dele
        preparedStatement.setString(1,name);
        preparedStatement.setInt(2,id);

        //执行
        int n = preparedStatement.executeUpdate();
        //查看执行的行数
        System.out.println("n = "+ n);

        //完成之后, 就需要关闭释放相关资源.
        preparedStatement.close();
        connection.close();

    }
}


